let App = getApp();

Page({
	data: {
		list: [],
		default_id: null,
		options: []
	},

	onLoad: function(options) {
		this.setData({
			options
		});
	},

	onShow: function() {
		// 获取收货地址列表
		this.getAddressList();
	},

	bindGoods(e) {
		let address_id = e.currentTarget.dataset.id;
		if (this.data.options.type == "buyNow") {
			wx.navigateTo({
				url: "/pages/placeorder/index?address_id=" + address_id
			});
		}
		if (this.data.options.type == "cartNow") {
			wx.navigateTo({
				url: "/pages/flow/checkout?address_id=" + address_id
			});
		}
		// 积分购买确认订单选择地址
		if (this.data.options.type == "jfNow") {
			wx.navigateTo({
				url: "/pages/jifen/sureOrder/index?address_id=" + address_id
			});
		}
	},

	/**
	 * 获取收货地址列表
	 */
	getAddressList: function() {
		let _this = this;
		App._get('address/lists', {}, function(result) {
			_this.setData(result.data);
		});
	},

	/**
	 * 添加新地址
	 */
	createAddress: function() {
		wx.navigateTo({
			url: './create?type=1&types=' + this.data.options.type
		});
	},

	/**
	 * 编辑地址
	 */
	editAddress: function(e) {
		wx.navigateTo({
			url: "./create?address_id=" + e.currentTarget.dataset.id + "&type=2&types=" + this.data.options.type
		});
	},

	/**
	 * 移除收货地址
	 */
	removeAddress: function(e) {
		let _this = this,
			address_id = e.currentTarget.dataset.id;
		wx.showModal({
			title: "提示",
			content: "您确定要移除当前收货地址吗?",
			success: function(o) {
				o.confirm && App._post_form('address/delete', {
					address_id
				}, function(result) {
					_this.getAddressList();
				});
			}
		});
	},

	/**
	 * 设置为默认地址
	 */
	setDefault: function(e) {
		let _this = this,
			address_id = e.currentTarget.dataset.id;
		_this.setData({
			default_id: parseInt(address_id)
		});
		App._post_form('address/setDefault', {
			address_id
		}, function(result) {

		});
		return false;
	},

});
